import {query} from "../services/example";
import CookieConfig from '../assets/js/CookieConfig'
import { routerRedux } from 'dva/router'
export default {
    namespace:'app',
    state:{
        userRole:'',
        userInfo:{},
        menuList:[],
        locationPathname: '',
        locationQuery: {},
    },
    subscriptions: {
        setupHistory({ dispatch, history }){
            history.listen((location)=>{
                    dispatch({
                        type:'updateState',
                        payload: {
                            locationPathname: location.pathname,
                            locationQuery:location.search
                        },
                    })
            })
        },
        setup({dispatch}){
            dispatch({type:'query'});
        }
    },
    effects:{
        *query({payload},{call,put}){
            if(CookieConfig.isLogin()){
                const userRole = CookieConfig.getCookie({name:'token'});
                const data = yield call(query,payload={role:userRole})
                if(data.message){
                    yield put({
                        type:'querySuccess',
                        payload:{
                            menuList:data.data
                        }
                    })
                }else{
                    throw data
                }
            }else{
                yield put(routerRedux.push({
                    pathname: '/login',
                }))
            }

        }
    },
    reducers:{
        updateState (state, { payload }) {
            return {...state, ...payload}
        },
        querySuccess(state,{payload}){
            return {...state,...payload};
        }
    }

}
